<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/7/14
 * Time: 10:36
 */

namespace App\Rep;


use App\Models\Models;
use App\Models\Order;
use App\Models\OrderParent;
use App\Models\OrderSecond;

class OrderSecondRep
{

    //添加订单
    public function addOrderSecondRep($order_id,$modelid,$product_id,$model_number_id,$specification_id,$color_id,$is_replenishment,$is_stock_up,$num,$total_num,$is_kucun=2,$warehouse_id,$goods_shelf_id,$production_status = 0,$warehouse_location_id){
        $order_second = OrderSecond::where(['order_id'=>$order_id,'modelid'=>$modelid])->first();
        if($order_second){
            //添加和编辑时是否使用库存不一致
            if($order_second->is_kucun != $is_kucun){
                //当前只考虑：添加时使用了库存，编辑时不使用库存
                if($is_kucun == 2){
                    //初始化库存
                    (new OrderRep())->releaseOrderKucunLog($order_id,$order_second->id);
                }
            }
        }

        $insert_arr = [
            'order_id'=>$order_id,
            'modelid'=>$modelid??'',
            'product_id'=>$product_id,
            'model_number_id'=>$model_number_id,
            'specification_id'=>$specification_id,
            'color_id'=>$color_id,
            'is_replenishment'=>$is_replenishment,
            'is_stock_up'=>$is_stock_up,
            'num'=>$num,
            'total_num'=>$total_num,
            'is_kucun'=>$is_kucun,
            'production_status'=>$production_status,
            'warehouse_id'=>$warehouse_id,
            'goods_shelf_id'=>$goods_shelf_id,
            'warehouse_location_id'=>$warehouse_location_id
        ];
        $order_second = OrderSecond::updateOrCreate(
            ['order_id'=>$order_id,'modelid'=>$modelid],
            $insert_arr
        );
        return $order_second->id;
    }


    //获取产品id
    public function getOrderSecondProductIdRep($order_id,$modelid){
        $info = OrderSecond::where('order_id',$order_id)->where('modelid',$modelid)->first();
        return $info->product_id;
    }

    public function getModelnameByOrderSecondId($order_second_id){
        $info = OrderSecond::where('id',$order_second_id)->first();
        $modelName = '';
        if($info){
            $modelName = Models::where('modelid',$info->modelid)->value('modelname');
        }
        return $modelName;
    }
}
